//
//  updatePasswordView.m
//  zhangGuangTianXia
//
//  Created by tangtangchitangtang on 2017/11/8.
//  Copyright © 2017年 tangtangchitangtang. All rights reserved.
//

#import "updatePasswordView.h"
#import "loginViewController.h"

@implementation updatePasswordView
-(void)creatView{
    
    UIImageView *imageView=[UIImageView new];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    
    
    UIView *textView=[UIView new];
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(100);
    }];
    textView.backgroundColor=ColorWithRGB(240, 240, 240);
    textView.layer.cornerRadius=25;
    
    
    _phoneText=[UITextField new];
    [textView addSubview:_phoneText];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
    }];
    _phoneText.placeholder=@"手机号码";
    _phoneText.textColor=ColorWithRGB(49, 49, 49);
    _phoneText.font=[UIFont systemFontOfSize:17];
    _phoneText.returnKeyType=UIReturnKeyDone;
    _phoneText.keyboardType=UIKeyboardTypePhonePad;
    
    UIView *textView2=[UIView new];
    [self addSubview:textView2];
    [textView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(textView.mas_bottom).mas_equalTo(20);
    }];
    textView2.backgroundColor=ColorWithRGB(240, 240, 240);
    textView2.layer.cornerRadius=25;
    
    
    _codeLabel=[UILabel new];
    [textView2 addSubview:_codeLabel];
    [_codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(0);
    }];
    _codeLabel.textColor=_tianLanColor;
    _codeLabel.font=[UIFont systemFontOfSize:14];
    _codeLabel.text=@"获取验证码";
    [_codeLabel layoutIfNeeded];
    _codeLabel.userInteractionEnabled=YES;
    UITapGestureRecognizer *huoQuYan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(huoQuYanZhen:)];
    [_codeLabel addGestureRecognizer:huoQuYan];
    
    _codeText=[UITextField new];
    [textView2 addSubview:_codeText];
    [_codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
    }];
    _codeText.placeholder=@"短信验证码";
    _codeText.textColor=ColorWithRGB(49, 49, 49);
    _codeText.font=[UIFont systemFontOfSize:17];
    _codeText.keyboardType=UIKeyboardTypePhonePad;
    _codeText.delegate=self;
    _codeText.returnKeyType=UIReturnKeyDone;
    
    
    UIView *textView3=[UIView new];
    [self addSubview:textView3];
    [textView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(textView2.mas_bottom).mas_equalTo(20);
    }];
    textView3.backgroundColor=ColorWithRGB(240, 240, 240);
    textView3.layer.cornerRadius=25;
    
    _passwordText=[UITextField new];
    [textView3 addSubview:_passwordText];
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
    }];
    _passwordText.placeholder=@"设置密码";
    _passwordText.textColor=ColorWithRGB(49, 49, 49);
    _passwordText.font=[UIFont systemFontOfSize:17];
    _passwordText.keyboardType=UIKeyboardTypeNamePhonePad;
    _passwordText.delegate=self;
    _passwordText.secureTextEntry=YES;//密文
    _passwordText.returnKeyType=UIReturnKeyDone;
    
    
    UIView *textView4=[UIView new];
    [self addSubview:textView4];
    [textView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(textView3.mas_bottom).mas_equalTo(20);
    }];
    textView4.backgroundColor=ColorWithRGB(240, 240, 240);
    textView4.layer.cornerRadius=25;
    
    
    _passwordText2=[UITextField new];
    [textView4 addSubview:_passwordText2];
    [_passwordText2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
    }];
    _passwordText2.placeholder=@"确认密码";
    _passwordText2.textColor=ColorWithRGB(49, 49, 49);
    _passwordText2.font=[UIFont systemFontOfSize:17];
    _passwordText2.keyboardType=UIKeyboardTypeNamePhonePad;
    _passwordText2.delegate=self;
    _passwordText2.secureTextEntry=YES;//密文
    _passwordText2.returnKeyType=UIReturnKeyDone;
    
    
    
    UIButton *btn=[UIButton new];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(textView4);
        make.top.mas_equalTo(textView4.mas_bottom).mas_equalTo(40);
        make.height.mas_equalTo(50);
    }];
    btn.backgroundColor=_tianLanColor;
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn setTitle:@"确定" forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:17];
    btn.layer.cornerRadius=25;
    [btn addTarget:self action:@selector(registerBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

-(IBAction)registerBtn:(id)sender{
    NSString *phoneStr=_phoneText.text;
    if (![ZQTools isMobileNumber:phoneStr]) {
        [ZQTools svpInfo:@"手机号有误"];
        return;
    }
    NSString *codeStr=_codeText.text;
    if ([ZQTools charIsNil:codeStr]) {
        [ZQTools svpInfo:@"验证码不能为空"];
        return;
    }
    NSString *passwordStr=_passwordText.text;
    if ([ZQTools charIsNil:passwordStr]||passwordStr.length<6||passwordStr.length>17) {
        [ZQTools svpInfo:@"密码必须是6到17位字符组成"];
        return;
    }
    NSString *passwordStr2=_passwordText2.text;
    if (![passwordStr isEqualToString:passwordStr2]) {
        [ZQTools svpInfo:@"两次密码不一致"];
        return;
    }
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:phoneStr,@"phone",passwordStr,@"password",codeStr,@"code",nil];
    
    [ZQTools AFNPostDataUrl:@"suser/backPasswordOne" Dict:dic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
        BaseModel *dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
        if ([dataModel.status isEqualToString:@"200"]) {
            
            loginViewController *loging=[[loginViewController alloc] init];
            loging.fanhui=2;
            [ZQTools pushNextViewController:self.viewController andRootController:loging];
            
        }
        else{
            [ZQTools svpInfo:dataModel.msg];
        }
        
    } anderrorBlock:nil];
    
    
    
}


-(void)huoQuYanZhen:(UITapGestureRecognizer *)tap{
    
    NSString *phoneStr=_phoneText.text;
    if (![ZQTools isMobileNumber:phoneStr]) {
        [ZQTools svpInfo:@"手机号有误"];
        return;
    }
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:phoneStr,@"phone",@"MODIFYPASSWORD",@"type",nil];
    [ZQTools AFNPostDataUrl:@"msmscode/setSmscode" Dict:dict andTableView:nil andView:nil andSuccessBlock:^(id responseObject) {
        BaseModel *dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
        if ([dataModel.status isEqualToString:@"200"]) {
            _codeLabel.userInteractionEnabled=NO;
            _timeTick=60;
            _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        }
        else{
            [ZQTools svpInfo:dataModel.msg];
        }
        
    } anderrorBlock:nil];
    
}
-(void)timeFireMethod
{
    _codeLabel.userInteractionEnabled=NO;
    if (_timeTick==0) {
        _codeLabel.userInteractionEnabled=YES;
        _codeLabel.text=@"获取验证码";
        [_timer invalidate];
        return;
    }
    _timeTick--;
    _codeLabel.text=[NSString stringWithFormat:@"重新发送(%dS)",_timeTick];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
